#include "test_sql.h"

void test_sql::testInsertAndQueryTeacher() {
    SqlHelper &dbHelper = SqlHelper::getInstance();
    // Insert a sample teacher
    if (dbHelper.insertTeacher("John Doe", "Engineering", "Computer Science", "Professor")) {
        qDebug() << "Successfully inserted teacher.";
    }
    // Query and display teachers
    QList<QVariantList> teachers = dbHelper.queryTeachers();
    qDebug() << "Teachers:";
    for (const auto &teacher : teachers) {
        qDebug() << "Name:" << teacher[0].toString()
                 << ", College:" << teacher[1].toString()
                 << ", Department:" << teacher[2].toString()
                 << ", Title:" << teacher[3].toString();
    }
}
void test_sql::testInsertAndQueryCourse() {
    SqlHelper &dbHelper = SqlHelper::getInstance();
    // Insert a sample course
    if (dbHelper.insertCourse("CS101", "Introduction to Programming", "Core", "Fall 2023", 4, 30, "John Doe")) {
        qDebug() << "Successfully inserted course.";
    }
    // Query and display courses
    QList<QVariantList> courses = dbHelper.queryCourses();
    qDebug() << "Courses:";
    for (const auto &course : courses) {
        qDebug() << "Code:" << course[0].toString()
                 << ", Name:" << course[1].toString()
                 << ", Type:" << course[2].toString()
                 << ", Semester:" << course[3].toString()
                 << ", Credits:" << course[4].toInt()
                 << ", Student Count:" << course[5].toInt()
                 << ", Teacher Name:" << course[6].toString();
    }
}
void test_sql::testInsertAndQueryEvaluation() {
    SqlHelper &dbHelper = SqlHelper::getInstance();
    // Insert a sample evaluation (ensure teacher and course IDs exist)
    if (dbHelper.insertEvaluation("CS101", 3, 4, 5, 3)) {
        qDebug() << "Successfully inserted evaluation.";
    }
    // Query and display evaluations
    QList<QVariantList> evaluations = dbHelper.queryEvaluations();
    qDebug() << "Evaluations:";
    for (const auto &evaluation : evaluations) {
        qDebug() << "Course Code:" << evaluation[0].toString()
                 << ", Self Evaluation:" << evaluation[1].toInt()
                 << ", Student Evaluation:" << evaluation[2].toInt()
                 << ", Supervisor Evaluation:" << evaluation[3].toInt()
                 << ", Peer Evaluation:" << evaluation[4].toInt();
    }
}
void test_sql::testInsertAndQueryStudent() {
    SqlHelper &dbHelper = SqlHelper::getInstance();
    // Insert a sample student
    if (dbHelper.insertStudent("user", "CS101, MATH202")) {
        qDebug() << "Successfully inserted student.";
    }
    // Query and display students
    QList<QVariantList> students = dbHelper.queryStudents();
    qDebug() << "Students:";
    for (const auto &student : students) {
        qDebug() << "Name:" << student[0].toString()
                 << ", Enrolled Courses:" << student[1].toString();
    }
}

void test_sql::testInsertAndQueryUser() {
    SqlHelper &dbHelper = SqlHelper::getInstance();
    // Insert a sample user
    if (dbHelper.insertUser("user2", "1234", "user")) {
        qDebug() << "Successfully inserted user.";
    }
    // Query and display users
    QList<QVariantList> users = dbHelper.queryUsers();
    qDebug() << "Users:";
    for (const auto &user : users) {
        qDebug() << "Username:" << user[0].toString()
                 << ", Password:" << user[1].toString()
                 << ", User Type:" << user[2].toString();
    }
    // Demonstrate login
    UserSession &session = UserSession::getInstance();
    session.loginUser("user", "user");
    if (session.isLoggedIn()) {
        qDebug() << "Current logged-in user:" << session.getCurrentUsername();
    }
    // Logout
    session.logoutUser();
}

void test_sql::testQueryCoursesForUser() {
    SqlHelper &dbHelper = SqlHelper::getInstance();
    QList<QPair<QString, QString>> courses = dbHelper.queryCoursesForUser("user");

    if (courses.isEmpty())
    {
        qDebug() << "No courses found for user";
    }
    else
    {
        qDebug() << "Courses for user";
        for (const auto &course : courses)
            qDebug() << "Course Name:" << course.first << ", Course Code:" << course.second;
    }
}

void test_sql::test_query_teacher_by_name()
{
    SqlHelper &dbHelper = SqlHelper::getInstance();
    QVariantList teacherDetails = dbHelper.queryTeacherByName("John Doe");
    if (teacherDetails.isEmpty()) {
        qDebug() << "No teacher found with name:" << "John Doe";
    } else {
        qDebug() << "Teacher details for name:" << "John Doe";
        qDebug() << "ID:" << teacherDetails.value(0).toInt();
        qDebug() << "Name:" << teacherDetails.value(1).toString();
        qDebug() << "College:" << teacherDetails.value(2).toString();
        qDebug() << "Department:" << teacherDetails.value(3).toString();
        qDebug() << "Title:" << teacherDetails.value(4).toString();
    }
}

void test_sql::test_query_course_by_course_code()
{
    SqlHelper &dbHelper = SqlHelper::getInstance();
    QVariantList courseDetails = dbHelper.queryCourseByCourseCode("CS101");
    if (courseDetails.isEmpty()) {
        qDebug() << "No course found with code:" << "CS101";
    } else {
        qDebug() << "Course details for code:" << "CS101";
        qDebug() << "Name:" << courseDetails.value(0).toString();
        qDebug() << "Type:" << courseDetails.value(1).toString();
        qDebug() << "Semester:" << courseDetails.value(2).toString();
        qDebug() << "Credits:" << courseDetails.value(3).toInt();
        qDebug() << "Student Count:" << courseDetails.value(4).toInt();
        qDebug() << "Teacher Name:" << courseDetails.value(5).toString();
    }
}

